﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using YFIN.Admin.ProductServiceRef;
using Microsoft.Expression.Interactivity.Core;
using YFIN.Admin.Admin;

namespace YFIN.Admin.ViewModels.Admin
{
    public class ProductManageVM : INotifyPropertyChanged
    {
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
        #region 属性
        private ObservableCollection<ModelProduct> _products;
        public ObservableCollection<ModelProduct> Products
        {
            get
            {
                return _products;
            }
            set
            {
                if (_products != value)
                {
                    _products = value;
                    NotifyPropertyChanged("Products");
                }
            }
        }

        private ModelProduct _product;
        public ModelProduct Product
        {
            get
            {
                return _product;
            }
            set
            {
                if (_product != value)
                {
                    _product = value;
                    NotifyPropertyChanged("Product");
                }
            }
        }

        public ICommand AddCommand { get; private set; }
        public ICommand UpdateCommand { get; private set; }
        public ICommand DeleteCommand { get; private set; }
        #endregion
         public ProductManageVM()
        {
            AddCommand = new ActionCommand(Add);
            UpdateCommand = new ActionCommand(Update);
            DeleteCommand = new ActionCommand(Delete);
            //TODO:其他初始化
            var client = new ProductServiceClient();
            client.GetAllCompleted +=new EventHandler<GetAllCompletedEventArgs>(client_GetAllCompleted);
            client.GetAllAsync();
        }

        public void Add()
        {
            Product = new ModelProduct { ExpireDate = DateTime.Now, StartDate =DateTime.Now, RiskLevel = null};
            var m = new ProductManageDetail { DataContext = Product };
            m.Show();
            m.Closed +=
                (sender, e) =>
                {
                    if (m.DialogResult.HasValue && m.DialogResult.Value)
                    {
                        var client = new ProductServiceClient();
                        client.AddCompleted+=new EventHandler<AddCompletedEventArgs>(client_AddCompleted);
                        client.AddAsync(Product);
                    }
                };
        }

        public void Update(object selectedItem)
        {
            Product = (ModelProduct)selectedItem;
            var m = new ProductManageDetail { DataContext = Product };
            m.Show();
            m.Closed +=
                (sender, e) =>
                {
                    if (m.DialogResult.HasValue && m.DialogResult.Value)
                    {
                        var client = new ProductServiceClient();
                        client.UpdateCompleted += new EventHandler<UpdateCompletedEventArgs>(client_UpdateCompleted);
                        client.UpdateAsync(Product);
                    }
                };
        }

        public void Delete()
        {
            var client = new ProductServiceClient();
            client.DeleteCompleted += new EventHandler<DeleteCompletedEventArgs>(client_DeleteCompleted);
            client.DeleteAsync(Product);
        }

        private void client_DeleteCompleted(object sender, ProductServiceRef.DeleteCompletedEventArgs e)
        {
            if (e.Result)
            {
                Products.Remove(Product);
                Product = null;
            }
        }

        private void client_AddCompleted(object sender, ProductServiceRef.AddCompletedEventArgs e)
        {
            if (e.Result)
            {
                var client = new ProductServiceClient();
                client.GetAllCompleted +=new EventHandler<GetAllCompletedEventArgs>(client_GetAllCompleted2);
                client.GetAllAsync();
            }
        }

        private void client_GetAllCompleted(object sender, ProductServiceRef.GetAllCompletedEventArgs e)
        {
            Products = e.Result;
        }

        private void client_GetAllCompleted2(object sender, ProductServiceRef.GetAllCompletedEventArgs e)
        {
            if(e.Result != null && e.Result.Count > 0)
            {
                Products.Add(e.Result[e.Result.Count - 1]);
            }
        }

        private void client_UpdateCompleted(object sender, ProductServiceRef.UpdateCompletedEventArgs e)
        {
            if (e.Result)
            {
            }
        }

    }
}
